package com.hmsm.sys.po;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hmsm.server.util.MD5;
import com.hmsm.sys.bean.LocalRole;
import com.hmsm.sys.bean.LocalUser;

/**
 * UserInfo entity. @author MyEclipse Persistence Tools
 */

public class UserInfo extends LocalUser<DeptInfo, UserInfo, RoleInfo> implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5219660239443417796L;

	// Fields
	private Integer pkId;
	private String idType;
	private Integer state;
	private String name;//用户姓名
	private String userName;//用户账号
	private String passWord;
	private String idCard;//身份证号码
	private String userDept;
	private String telPhone;
	private String email;
	private String address;
	private Integer type;
	private Timestamp createDate;
    private Integer snCode;//动态密令sn号
    private String remark;		// 注解、警告
    
    private Set<UserOrg> userOrgs = new HashSet<UserOrg>();

	// Constructors

	public Set<UserOrg> getUserOrgs() {
		return userOrgs;
	}

	public void setUserOrgs(Set<UserOrg> userOrgs) {
		this.userOrgs = userOrgs;
	}
	
	private List<DeptInfo> userDepts; 

	private DeptInfo depart;

	public DeptInfo getDepart() {
		return depart;
	}

	public void setDepart(DeptInfo depart) {
		this.depart = depart;
		updatUserDept();
	}
	
	private OrgInfo org;

	public OrgInfo getOrg() {
		return org;
	}

	public void setOrg(OrgInfo org) {
		this.org = org;
	}
	
	private List<RoleInfo> roles;
	
	public List<RoleInfo> getRoles() {
		return roles;
	}

	@Override
	public void setRoles(List<RoleInfo> roles) {
		this.roles = roles;
	}
	
	public List<Integer> getRoleIds(){
		List<Integer> list = null;
		
		if (roles!=null){
			list = new ArrayList<Integer>();
			for (LocalRole role:roles){
				list.add(role.getPkId());
			}
		}
		
		return list;
	}
	
	public void setRoleIds(List<Integer> ids){
		if (ids==null){
			roles = null;
		}else{
			roles = new ArrayList<RoleInfo>();
			for (Integer id:ids){
				RoleInfo role = new RoleInfo();
				role.setPkId(id);
				roles.add(role);
			}
		}
	}

	private Map<String, List<? extends Object>> refObject;
	
	public Map<String, List<? extends Object>> getRefObject() {
		return refObject;
	}

	public void setRefObject(Map<String, List<? extends Object>> refObject) {
		this.refObject = refObject;
	}

	/** default constructor */
	public UserInfo() {
	}

	/** minimal constructor */
	public UserInfo(Integer pkId, String name) {
		this.pkId = pkId;
		this.name = name;
	}

	/** minimal constructor */
	public UserInfo(String idType, Integer state, String name, String passWord, Integer type, Timestamp createDate) {
		this.idType = idType;
		this.state = state;
		this.name = name;
		this.passWord = passWord;
		this.type = type;
		this.createDate = createDate;
	}

	/** full constructor */
	public UserInfo(String idType, Integer state, String name, String userName, String passWord, String idCard, String userDept, String telPhone, String email, String address,
	Integer type, Timestamp createDate,Integer snCode) {
		this.idType = idType;
		this.state = state;
		this.name = name;
		this.userName = userName;
		this.passWord = passWord;
		this.idCard = idCard;
		this.userDept = userDept;
		this.telPhone = telPhone;
		this.email = email;
		this.address = address;
		this.type = type;
		this.createDate = createDate;
		this.snCode = snCode;
	}

	// Property accessors

	public Integer getPkId() {
		return this.pkId;
	}

	public void setPkId(Integer pkId) {
		this.pkId = pkId;
	}

	public String getIdType() {
		return this.idType;
	}

	public void setIdType(String idType) {
		this.idType = idType;
	}

	public Integer getState() {
		return this.state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return this.passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getIdCard() {
		return this.idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getUserDept() {
		return this.userDept;
	}

	public void setUserDept(String userDept) {
		this.userDept = userDept;
	}

	public String getTelPhone() {
		return this.telPhone;
	}

	public void setTelPhone(String telPhone) {
		this.telPhone = telPhone;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Timestamp getCreateDate() {
		return this.createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	public Integer getSnCode() {
		return snCode;
	}

	public void setSnCode(Integer snCode) {
		this.snCode = snCode;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getDeptId() {
		Integer deptId = null;
		
		if (depart!=null){
			deptId = depart.getPkId();
		}else{
			if (userOrgs!=null){
				for(UserOrg uo:userOrgs){
					deptId = uo.getDeptId();
					break;
				}
			}
		}
		
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		if (depart==null){
			depart = new DeptInfo();
		}
		depart.setPkId(deptId);
	}

	public boolean validatePassword(String password) {
		boolean result = false;
		
		if(this.passWord!=null&&password!=null){
			if(!MD5.isMD5String(password)){
				password = MD5.getMD5(password);
			}
			result = passWord.equals(password);
			if(!result){
				result = passWord.equals(MD5.getMD5(password));
			}
		}
		
		return result; 
	}

	public void updatePassword(String password) {
		if(MD5.isMD5String(password)){
			passWord = password;
		}else{
			passWord = MD5.getMD5(password);
		}
	}

	public void resetPassword() {
		updatePassword("8888");
	}
	
	public String toString(){
		return name+(userName==null?"":(userName.equals(name)?"":"("+userName+")"));
	}

	public void updatUserDept() {
		if (getUserDepts()!=null){
			String userDept = "";
			Map<Integer, List<DeptInfo>> orgMap = new HashMap<Integer, List<DeptInfo>>();
			boolean isFirst = true;
			for(DeptInfo dept:getUserDepts()){
				
				if(userDept.length()>0){
					userDept += ";";
				}
				if(isFirst){
					isFirst = false;
					userDept += dept.getDeptPathName(1);
				}else{
					userDept += dept.getDeptPathName(0);
				}
			}
			this.userDept = userDept;
		}
	}

	@Override
	public boolean affiliatedWithDepart(DeptInfo depart) {
		boolean result = false;
		
		if (userDepts!=null){
			for(DeptInfo userDept: userDepts){
				if (userDept!=null&&userDept.getPkId()!=null&&depart!=null&&userDept.getPkId().equals(depart.getPkId())){
					result = true;
					break;
				}
			}
		}
		
		return result;
	}

	@Override
	public List<DeptInfo> getUserDepts() {
		return userDepts;
	}

	@Override
	public void setUserDepts(List<DeptInfo> userDepts) {
		this.userDepts = userDepts;
	}

	@Override
	public boolean isSysAdmin() {
		return type!=null&&type==2;
	}

	@Override
	public boolean isSuperAdmin() {
		return type!=null&&type==2&&name.equals("超级管理员");
	}

}